<?php
include_once("../../includes/config.inc.php");
check_login();
$suserSession=get_suser();
$customer_id = $_SESSION['customer_id'] = $suserSession['id'];
$act = $_POST['act'];					//var
$aid = $_POST['id'];


//全部过程 START
$article = getSendArticle($act,$aid);		//得到发送内容

$format  = getSendFormat($article,$act);	//转换发送格式

send($format);								//发送
//全部过程 END


function send($info){			//发送
	$token = getToken();
	$sendUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=$token";
	https_request($sendUrl,$info);
}


function getSendArticle($act,$aid){			//得到发送内容
	$commonModel = new Model_InfoCommon();	//model
	switch ($act){
		case 'text':	$type = 1;		break;
		case 'single':	$type = 2;		break;
		default : 		$type = 3;		break;
	}
	$filter['where'] = "id = '$aid' and info_type = '$type'";
	$sql = $commonModel->select($filter);
	$article = $commonModel->fetchRow($sql);
	return $article;
}


function getSendFormat($article,$type){		//格式转换
	$info = array();
	$info['msgtype'] = $type=='text' ? 'text' : 'mpnews';
	
	//if($type=='text')
	
	switch ($type){
		
		case 'text':		//文字格式
			$info['filter'] = array('is_to_all'=>true);//,'group_id'=>2
			$info['text'] = array('content'=>$article['info_intro']);
			break;
			
		default:			//图文格式
			
			$info['filter'] = array('is_to_all'=>true);//,'group_id'=>2
			$media_id		= Model_Table::get('Mass')->get_multi_media_id($article);
			
			dump($media_id);
			$info['mpnews'] = array('media_id'=>$media_id);
			break;
	}
	return json_encode($info);
}


function getToken(){		//获取全局TOKEN

	//检验可用token
	$configModel = new Model_CustomerConfig();
	$filter['where']  = "customer_id='$customer_id' and c_type='token'";
	$filter['order']  = "create_date asc";
	$sql = $configModel->select($filter);
	$result = $configModel->fetchRow($sql);
	
	if($result){
		if($result['valid_time']>time && $result['c_value']){
			//token、可用
			$token = $result['c_value'];	
		}else{
			//调用token接口
			$token = callTokenApi();			
			save_token($token,$configModel);
		}
	}else{
		//调用token接口
		$token = callTokenApi();				
		save_token($token,$configModel,'insert');
		
	}
	return $token;
}


function https_request($url, $data = null)		//https请求（支持GET和POST）
{
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	if (!empty($data)){
	 curl_setopt($curl, CURLOPT_POST, 1);
	 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	}
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
	$output = curl_exec($curl);
	curl_close($curl);
	return $output;
}

function callTokenApi(){			
	$appId = "wxed1cf9dbf93544a8";
	$secret= "1618454a01e18395b48dd72d1b3e1bc0";
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$secret";
	$tokenInfo = json_decode(file_get_contents($url));
	$token = $tokenInfo->access_token;
	return $token;
}

function save_token($token=false,$configModel,$case=false){
	
	$data['c_value']	     = $token;
	$data['valid_time']	     = time()+5000;
	
	if($case){
		$data['customer_id']	 = $_SESSION['customer_id'];
		$data['c_type']	 		 = 'token';
		$configModel->insert($data);
	}else{
		$configModel->row_update($data,"customer_id='{$_SESSION['customer_id']}' and c_type='token'");
	}
}